import { createRouter, createWebHashHistory } from "vue-router";
import { toast } from "@/utils/index.js";
import { getCookies } from "@/utils/auth.js";
import nProgress from "nprogress";
import "nprogress/nprogress.css";

import { useAdminStore } from "@/store/admin.js";
import pinia from "@/store/index.js";

const store = useAdminStore(pinia);

//基础路由
const routes = [
  {
    path: "/",
    name: "admin",
    component: () => import("../views/layout/Layout.vue"),
  },
  {
    path: "/login",
    name: "login",
    meta: {
      title: "登录",
    },
    component: () => import("../views/Login.vue"),
  },
  {
    path: "/:pathMatch(.*)*",
    component: () => import("../views/Notfound.vue"),
  },
];

function generateAsyncRoutes(menus) {
  let hasNewsR = false;
  function mapRoute(menus) {
    menus.forEach((arr) => {
      //一级菜单
      const hasR = asyncRoutes.find((asyncRoute) => {
        return asyncRoute.path === arr.frontpath;
      });
      if (hasR && !router.hasRoute(arr.frontpath)) {
        router.addRoute("admin", hasR);
        hasNewsR = true;
      }
      if (arr.child && arr.child.length > 0) {
        //再次遍历子路由
        mapRoute(arr.child);
      }
    });
    // console.log(router.getRoutes(), "-------------");
  }
  mapRoute(menus);

  return hasNewsR;
}

const asyncRoutes = [
  {
    path: "/",
    name: "/",
    meta: {
      title: "主控台",
    },
    component: () => import("../views/Home.vue"),
  },
  {
    path: "/goods/list",
    name: "/goods/list",
    meta: {
      title: "商品管理",
    },
    component: () => import("../views/child/GoodsList.vue"),
  },
  {
    path: "/category/list",
    name: "/category/list",
    meta: {
      title: "分类管理",
    },
    component: () => import("../views/child/CategoryList.vue"),
  },
  {
    path: "/skus/list",
    name: "/skus/list",
    meta: {
      title: "规格管理",
    },
    component: () => import("../views/child/SkusList.vue"),
  },
  {
    path: "/coupon/list",
    name: "/coupon/list",
    meta: {
      title: "优惠券管理",
    },
    component: () => import("../views/child/CouponList.vue"),
  },
  {
    path: "/user/list",
    name: "/user/list",
    meta: {
      title: "用户管理",
    },
    component: () => import("../views/child/UserList.vue"),
  },
  {
    path: "/level/list",
    name: "/level/list",
    meta: {
      title: "会员等级",
    },
    component: () => import("../views/child/LevelList.vue"),
  },
  {
    path: "/order/list",
    name: "/order/list",
    meta: {
      title: "订单管理",
    },
    component: () => import("../views/child/OrderList.vue"),
  },
  {
    path: "/comment/list",
    name: "/comment/list",
    meta: {
      title: "评论管理",
    },
    component: () => import("../views/child/CommentList.vue"),
  },
  {
    path: "/manager/list",
    name: "/manager/list",
    meta: {
      title: "管理员管理",
    },
    component: () => import("../views/child/ManagerList.vue"),
  },
  {
    path: "/access/list",
    name: "/access/list",
    meta: {
      title: "权限管理",
    },
    component: () => import("../views/child/AccessList.vue"),
  },
  {
    path: "/role/list",
    name: "/role/list",
    meta: {
      title: "角色管理",
    },
    component: () => import("../views/child/RoleList.vue"),
  },
  {
    path: "/setting/base",
    name: "/setting/base",
    meta: {
      title: "基础设置",
    },
    component: () => import("../views/child/SettingBase.vue"),
  },
  {
    path: "/setting/buy",
    name: "/setting/buy",
    meta: {
      title: "交易设置",
    },
    component: () => import("../views/child/SettingBuy.vue"),
  },
  {
    path: "/setting/ship",
    name: "/setting/ship",
    meta: {
      title: "物流设置",
    },
    component: () => import("../views/child/SettingShip.vue"),
  },
  {
    path: "/distribution/index",
    name: "/distribution/index",
    meta: {
      title: "分销员管理",
    },
    component: () => import("../views/child/DistributionIndex.vue"),
  },
  {
    path: "/distribution/setting",
    name: "/distribution/setting",
    meta: {
      title: "分销设置",
    },
    component: () => import("../views/child/DistributionSetting.vue"),
  },
  {
    path: "/image/list",
    name: "/image/list",
    meta: {
      title: "图库管理",
    },
    component: () => import("../views/child/ImageList.vue"),
  },
  {
    path: "/notice/list",
    name: "/notice/list",
    meta: {
      title: "公告管理",
    },
    component: () => import("../views/child/NoticeList.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

// 路由守卫
router.beforeEach(async (to, from, next) => {
  nProgress.start();
  const token = getCookies();
  if (to.meta) {
    document.title = to.meta.title + "-后台管理系统";
  }
  if (!token && to.path !== "/login") {
    toast("请先登录！", "error");
    return next("/login");
  }

  if (token && to.path == "/login") {
    toast("请勿重复登录！", "error");
    return next({
      path: from.path ? from.path : "/",
    });
  }
  let hasNewsR = false;
  if (token) {
    await store.getAdminInfo();

    // 调用动态路由添加
    hasNewsR = generateAsyncRoutes(store.adminInfo.menus);
  }
  hasNewsR ? next(to.fullPath) : next();
});
router.afterEach((to, from) => {
  nProgress.done();
});
export default router;
